package com.processmining.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.processmining.cache.RedisClient;
import com.processmining.config.PMConfig;
import com.processmining.dao.UserMapper;
import com.processmining.entity.User;
import com.processmining.service.IUserService;
import com.processmining.util.EncodeUtil;
import com.processmining.util.Mail;
import com.processmining.util.response.Code;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.processmining.dao.UserMapper;
import com.processmining.entity.User;
import com.processmining.service.IUserService;

/**
 * 用户管理业务层实现
 * 
 * @author hxuhao
 *
 */

@Service

@Transactional()

public class UserServiceImpl implements IUserService {

	@Autowired
	private UserMapper userDao;


	@Override
	public int register(User user, String context) {
		Integer isExistEmail=userDao.isExistEmail(user.getEmail());
		Integer isExistNickname=userDao.isExistNickname(user.getNickname());
		if(isExistEmail!=null){
			return Code.OCCUPIED_EMAIL;
		}else if(isExistNickname!=null){
			return Code.OCCUPIED_NICKNAME;
		} else {
			// user.setActivated(false);
			String pwd = user.getPassword();
			user.setPassword(EncodeUtil.string2MD5(pwd + PMConfig.SALT));
			int status = userDao.insertSelective(user);
			if (status != 1) {
				return Code.SYSTEM_ERROR;
			} else {
				try {
					Mail.sendMail("reao123@163.com", "1a2s3d4f", "smtp.163.com", user.getEmail(), user.getId(),
							context);
				} catch (Exception e) {
					return Code.SYSTEM_ERROR;
				}
				return Code.SUCCEED;
			}
		}
	}

	@Override
	public boolean login(User user) {
		String pwd = user.getPassword();
		user.setPassword(EncodeUtil.string2MD5(pwd + PMConfig.SALT));
		User u = userDao.selectByEmailAndPassword(user);
		// 如果返回的u不为null，u没有被封，u被激活
		if (u == null) {

			return false;
		}
		return !u.getBanned() || u.getActivated() ? true : false;
	}

	@Override
	public int updatePassword(User user, String newPassword) {
		User u = new User();
		u.setId(user.getId());
		u.setPassword(EncodeUtil.string2MD5(newPassword + PMConfig.SALT));
		// user.setPassword(EncodeUtil.string2MD5(newPassword+PMConfig.SALT));
		return userDao.updateByPrimaryKeySelective(u) == 1 ? Code.SUCCEED : Code.SYSTEM_ERROR;
	}

	@Override
	public int updateNickName(User user,String newNickname) {
		if(userDao.isExistNickname(newNickname)!=null){
			return Code.OCCUPIED_NICKNAME;
		}
		User u = new User();
		u.setId(user.getId());
		u.setNickname(newNickname);
		// user.setNickname(newNickname);
		return userDao.updateByPrimaryKeySelective(u) == 1 ? Code.SUCCEED : Code.SYSTEM_ERROR;
	}

	@Override
	public boolean activate(User user) {
		user.setActivated(true);
		return userDao.updateByPrimaryKey(user) == 1 ? true : false;
	}

	public User getUserById(Integer id) {
		return userDao.selectByPrimaryKey(id);
	}

	@Override
	public User getUserByEmailAndPassword(User user) {
		return userDao.selectByEmailAndPassword(user);
	}

}
